/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.pages;

import com.google.inject.Inject;
import nl.xs4all.dylanschell.deckprinter.model.CardInDeck;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.services.DeckParser;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.PropertyModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dylan
 * Date: 14-12-10
 * Time: 21:53
 * To change this template use File | Settings | File Templates.
 */
public class DeckEditPage extends BasePage
{
    @Inject
    public DeckParser parser;

    private DeckDescriptor deck = new DeckDescriptor("New Deck");

    public DeckEditPage(File f)
    {
        if (f != null)
        {
            try
            {
                FileInputStream fis = new FileInputStream(f);
                try
                {
                    deck = parser.parseDeck(fis, f.getName());
                } finally
                {
                    fis.close();
                }
            } catch (IOException e)
            {
                error("Unable to load deck: " + e.getMessage());
            }
        }
        initGui();
    }

    public DeckEditPage(DeckDescriptor deck)
    {
        this.deck = deck;
        initGui();
    }

    private void initGui()
    {
        final WebMarkupContainer actions = new WebMarkupContainer("actions");
        actions.setOutputMarkupId(true);
        actions.setOutputMarkupPlaceholderTag(true);
        add(actions);

        Link home = new BookmarkablePageLink("home", getApplication().getHomePage());
        actions.add(home);

        Form<Void> form = new Form<Void>("form");
        add(form);

        AjaxSubmitLink save = new AjaxSubmitLink("save", form)
        {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                setResponsePage(new DeckViewPage(deck));
            }
        };
        actions.add(save);

        // create a feedback panel
        final Component feedback = new FeedbackPanel("feedback").setOutputMarkupPlaceholderTag(true);
        add(feedback);

        RepeatingView rv = new RepeatingView("card");
        form.add(rv);
        List<CardInDeck> cards = deck.getCardsInSection(DeckDescriptor.SECTION_MAIN);
        for (CardInDeck cid : cards)
        {
            WebMarkupContainer container = new WebMarkupContainer(rv.newChildId());
            rv.add(container);
            TextField<Integer> qtyField = new TextField<Integer>("qty", new PropertyModel<Integer>(cid, "quantity"));
            TextField<String> nameField = new TextField<String>("name", new PropertyModel<String>(cid, "name"));
            container.add(qtyField);
            container.add(nameField);
        }
    }

    @Override
    public String getTitle()
    {
        return "Edit Deck";
    }
}
